/**
 * Created by king on 2017/6/21.
 */
(function() {
    ADF.actionModel.XYAlignCenterModel = function() {
        ADF.actionModel.XYAlignBottomModel.superclass.constructor.call(this);
        this.config = {text:"水平居中对齐",icon:"icon-youduiqi"};
        this.id = "XYAlignCenter";
        this.hotkeyInfo = {
            // type : "keydown",
            // key : "ArrowRight"
        }
    };

    COM.util.defineClass(ADF.actionModel.XYAlignCenterModel, AGEF.ActionModel, {
        execute: function(graphicalViewer){
            var selection = graphicalViewer.getSelection();

            var leftMin = null;
            var rightMax = null;
            var centerX = null;
            
            for(var i in selection){
                var selectionItem = selection[i];
                var bounds = selectionItem.model.getLayoutData();
                if(bounds instanceof COM.draw2d.Rectangle){
                    if(!leftMin || (leftMin && bounds.x < leftMin)){
                        leftMin = bounds.x;
                    }

                    if(!rightMax || (rightMax && bounds.x + bounds.width > rightMax)){
                        rightMax = bounds.x + bounds.width;
                    }
                }
            }

            if(leftMin && rightMax){
                centerX = leftMin + (rightMax - leftMin) / 2;
            }

            if(centerX){
                var map = {};
                var cmd = COM.gef.Command.createCommand({
                    execute: function execute() {
                        for(var i in selection){
                            var selectionItem = selection[i];
                            var model = selectionItem.model;
                            var bounds = model.getLayoutData();
                            map[model.id] = bounds;
                            
                            model.setLayoutData(new COM.draw2d.Rectangle(centerX - bounds.width / 2, bounds.y, bounds.width, bounds.height));
                        }
                    },
                    undo: function undo() {
                        for(var i in selection){
                            var selectionItem = selection[i];
                            var model = selectionItem.model;
                            model.setLayoutData(map[model.id]);
                        }
                    }
                });

                var commands = new COM.gef.CompoundCommand();
                commands.add(cmd);
                graphicalViewer.getEditDomain().getCommandStack().execute(commands);
            }
        },
        getEnabledState : function(graphicalViewer){
            var selection = graphicalViewer.getSelection();
            if(selection && selection.length > 1){
                var parent = null;
                for(var i in selection){
                    var selectionItem = selection[i];
                    if((parent && selectionItem.parent != parent) || !(selectionItem.model.getLayoutData() instanceof COM.draw2d.Rectangle)){
                        return false;
                    }else{
                        parent = selectionItem.parent;
                    }
                }
                return true;
            }else{
                return false;
            }
        }
    });
})();
